package com.chenzhe.apijieshou;

import com.chenzhe.apijieshou.util.InputToByteUtil;
import com.chenzhe.apijieshou.util.RSAUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.nio.ch.IOUtil;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chenzhe.apijieshou.util.base64.Base64 ;

@RestController
public class ApiController {
    public static  String privateKey  = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKgbeJOnKbicVLQIc/0H+jkoWr3Yig0woEyxTSC0eTgRSmzu1/zFgng15mvzascV50H/clqYWlUbaW2+8LjhYCMWd2GHs4MoXaoNAVUL5Y2tXK8wyfsKs8fJNmDFsMvkVsARlK4ZOF+9u8GaE5f/qZemiVBL4GDU4C4CG7XDxV/NAgMBAAECgYEAga63hMg28jh/S2cd+zu1N3S/E6dBUE2Ly74XLryqqqsEDCbwrgm7zXpfKDXe0HhAL3SdRfAz92WvHj9nOmh6nTAeWJQsctiXMcFJ52Es3VzyTwZSr/0yaWUNUC8fvPwXh80LFiwULxfXBnqoB2LXxAxhpHAXUS/TYnkS1qCXGuECQQD+JYI5XSw6JL1sQ9E/Ur6trRxkcBJSLRyHb8qnzfFva8RF6FfswxRdeggOB/PRDdMjMLGgd9yVZWrpS3wfl2l1AkEAqVVTwstvXgQhK5sf5ix7FfzQSEDxqJvxdMr6+zS8c2LW3nWt2p5dRpLXh7qNggkSJGpfqNo2i+LgzsmDKZn5+QJBALz7bUt0sjeGLGj+QPHCeNGywUMaZzU+IFHB2kNPi7Hbw/Zr4D3JjjI4VRcB6NbSXed0BRGbL6bT5F0YaXMgZyUCQGvSOy1z4AvbUUu00ND9Tbqoae83+yhDc7HMdZdy8vP94ZdxEyx7XiVL5xUzwlskb3rkiNi5abYU1tSVF4Fr2/ECQQCXoLmiNR81L1C24OgzWStgc/nX5eZUj2FTTXtRpheYGHHQ3ifSusP912OW7NdLQ6X4LJ7nLUcUcUZqycpTMPyM";
    public  static String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCoG3iTpym4nFS0CHP9B/o5KFq92IoNMKBMsU0gtHk4EUps7tf8xYJ4NeZr82rHFedB/3JamFpVG2ltvvC44WAjFndhh7ODKF2qDQFVC+WNrVyvMMn7CrPHyTZgxbDL5FbAEZSuGThfvbvBmhOX/6mXpolQS+Bg1OAuAhu1w8VfzQIDAQAB";
    @RequestMapping(value = "/api")

    public  String  getApi(HttpServletRequest request , HttpServletResponse response) throws Exception {
        String key = request.getParameter("key");
        String s = key.replace("%2B", "+");
        byte[] bytes1 = s.getBytes();
        byte[] bytes2 = Base64.decodeBase64(bytes1);
        byte[] decodedData = RSAUtils.decryptByPublicKey(bytes2, publicKey);
        ServletInputStream inputStream = request.getInputStream();
        byte[] bytes = InputToByteUtil.input2byte(inputStream);

        String target = new String(decodedData);//明文密码
        byte[] rawKey = AESUtil.getRawKey(target.getBytes());
        String decryptedPwd = AESUtil.decrypt(bytes, rawKey);//解密 数据
        System.out.println(target+"               我的 数据");
        return  "Success";
    }



}
